// 共分为3种情况,两个数组都增,两个数组都减,两个数组一增一减
/**
 * 两个数组都增的情况
 */
// var arr1 = [1, 3, 5, 7, 9]
// var arr2 = [0, 2, 4, 6, 8]
// 数组长度不相同的情况
// var arr1 = [1, 3, 5, 7]
// var arr2 = [0, 2, 4, 6, 8]

/**
 * 两个数组都减的情况
 */
// var arr1 = [9, 7, 5, 3, 1]
// var arr2 = [8, 6, 4, 2, 0]
// 数组长度不相同的情况
// var arr1 = [9, 7, 5, 3]
// var arr2 = [8, 6, 4, 2, 0]

/**
 * 1增2减的情况
 */
// var arr1 = [1, 3, 5, 7, 9]
// var arr2 = [8, 6, 4, 2, 0]
// 数组长度不相同的情况
// var arr1 = [1, 3, 5, 7]
// var arr2 = [8, 6, 4, 2, 0]

/**
 * 1减2增的情况
 */
// var arr1 = [9, 7, 5, 3, 1]
// var arr2 = [0, 2, 4, 6, 8]
// 数组长度不相同的情况
// var arr1 = [9, 7, 5, 3]
// var arr2 = [0, 2, 4, 6, 8]

function merge(arr1, arr2) {
    let res = []
    let allTime = arr1.length + arr2.length
    let nowTime = 1;
    let direction1 = arr1[0] > arr1[1] ? -1 : 1
    let direction2 = arr2[0] > arr2[1] ? -1 : 1
    let incraese1 = true
    let incraese2 = true
    let index1, index2
    if (direction1 == 1 && direction2 == 1) {
        index1 = index2 = 0
    } else if (direction1 == -1 && direction2 == -1) {
        index1 = arr1.length - 1
        index2 = arr2.length - 1
        incraese1 = incraese2 = false
    } else if (direction1 == 1 && direction2 == -1) {
        index1 = 0
        index2 = arr2.length - 1;
        incraese1 = true
        incraese2 = false
    } else {
        index1 = arr1.length - 1
        index2 = 0;
        incraese1 = false
        incraese2 = true
    }
    while (nowTime <= allTime) {
        if (arr1[index1] == undefined || arr2[index2] == undefined) {
            if (arr1[index1] != undefined) {
                res.push(arr1[index1++])
            } else {
                res.push(arr2[index2++])
            }
        } else {
            if (arr1[index1] > arr2[index2]) {
                res.push(arr2[index2])
                if ((!incraese1 && !incraese2) || (incraese1 && !incraese2)) {
                    index2--
                }
                else {
                    index2++
                }
            }
            else if (arr1[index1] < arr2[index2]) {
                res.push(arr1[index1])
                if ((incraese1 && incraese2) || (incraese1 && !incraese2)) {
                    index1++
                }
                else if (!incraese1 && !incraese2) {
                    index1--
                }
                else {
                    index1--
                }
            }
            else {
                res.push(arr1[index1])
                nowTime += 2
                if (incraese1 && incraese2) {
                    index1++ , index2++
                }
                else if (!incraese1 && !incraese2) {
                    index1-- , index2--
                }
                else if (incraese1 && !incraese2) {
                    index1++ , index2--
                }
                else {
                    index1-- , index2++
                }
                continue
            }
        }
        nowTime++
    }
    return res
}

let res = merge(arr1, arr2)
console.log(res)